/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

//article

//设置全局变量
var arColumn = 0;

//添加/修改文章
function addUptArticle(str){
	var insertId = $("#ar_id").val();
	var flag = $("#flag").val();
	if(str=='update'||str=='appupdate'){
		if(insertId==0&&flag=='add'){
			alert('错误的操作，您还未提交过任何文章！');
			return false;
		}else{
			var oldThumbPic = $("#old_small_pic").val();
		}
	}else{
		if(flag==1){
			alert('文章已经提交过了，请不要重复提交！');
			return false;
		}
	}
	if( check_column() == false || check_title() == false ) return false;
	var content=KE.util.getData('content');
	if(content==''){
		$("#check_content").html('<img height="16" width="16" border="0" src="images/admin/publish_x.png"/>内容不能为空！');
		return false;
	}else{
		$("#check_content").html('');
	}
	if(str!='add'){
		$("#sys_msg").text('系统提示：文章保存中，如果您选择了下载远程图片，系统处理时间可能会稍长，请等待！');
		$("#msg_box").show(800);
	}
	var cid=$("#column_id").val();
	var title=$("#title").val();
	var small_title=$("#small_title").val();
	var tags=$("#tags").val();
	var keywords=$("#keywords").val();
	var author=$("#author").val();
	var from=$("#from").val();
	var index_top=$("#index_top").attr('checked')==true?1:0;
	var column_top=$("#column_top").attr('checked')==true?1:0;
	var recommend=$("#recommend").attr('checked')==true?1:0;
	var title_color=$("#title_color").val();
	var sid=$("#special_id").val();
	var description=$("#description").val();
	var url_jump=$("#url_jump").val();
	var comment=$("#comment").attr('checked')?1:0;
	var money=$("#money").val();
	var add_time=$("#add_time").val();
	var validate=$("#validate").attr('checked')==true?1:0;
	var password=$("#password").val();
	var thumb_pic=$("#thumb_pic").val();
	var draft=$("#draft").attr('checked')==true?1:0;
	var down_pic=$("#down_pic").attr('checked')==true?1:0;
	var get_first_pic=$("#get_first_pic").attr('checked')==true?1:0;
	var contentData;
	var postUrl;
	if(str=='update'||str=='appupdate'){
		contentData={'ar_basic':{'cid':cid,'title':title,'small_title':small_title,'thumb_pic':thumb_pic,'tags':tags,'keywords':keywords,'author':author,'index_top':index_top,'from':from,'column_top':column_top,'recommend':recommend,'title_color':title_color,'sid':sid,'description':description,'url_jump':url_jump,'comment':comment,'money':money,'add_time':add_time,'validate':validate,'password':password,'draft':draft},'ar_content':{'old_thumb_pic':oldThumbPic,'down_pic':down_pic,'get_first_pic':get_first_pic,'content':content}};
		if(flag=='0'||flag=='add'){
			postUrl = base_url+'?m=article&a=updatear&id='+insertId+'&page=0';
		}else{
			postUrl = base_url+'?m=article&a=updatear&id='+insertId+'&page=1';
		}
	}else{
		contentData={'ar_basic':{'cid':cid,'title':title,'small_title':small_title,'thumb_pic':thumb_pic,'tags':tags,'keywords':keywords,'author':author,'index_top':index_top,'from':from,'column_top':column_top,'recommend':recommend,'title_color':title_color,'sid':sid,'description':description,'url_jump':url_jump,'comment':comment,'money':money,'add_time':add_time,'validate':validate,'password':password,'draft':draft},'ar_content':{'down_pic':down_pic,'get_first_pic':get_first_pic,'content':content}};
		postUrl = base_url+'?m=article&a=addarticle';
	}
	$.ajax({
		type: "POST",
		url: postUrl,
		data: {'data':JSON.stringify(contentData)},
		dataType:'json',
		success: function(rs){
			if(rs.ok==1){
				if(str=='add'){
					window.location=base_url+'?m=article&a=index';
				}else if(str=='apply'){
					$("#ar_id").val(rs.aid);
					$("#flag").val(1);
					$("#sys_msg").text('文章保存成功！');
				}else if(str=='appupdate'){
					window.location=base_url+'?m=article&a=index';
				}else if(str=='update'){
					$("#sys_msg").text('文章修改成功！');
				}
				arColumn=cid;
				$("#old_thumb_pic").val(thumb_pic);
				$("#tags").val(rs.tags);
				$("#keywords").val(rs.keywords);
				$("#description").val(rs.description);
				$("#msg_box").show(1000);
				setTimeout("$('#msg_box').hide(1000)",4000);
			}else{
				alert('文章保存失败，请联系管理员！');
				return false;
			}
		}
	});
}

function empty_form(){
	$("#flag").val(0)
	arColumn = 0;
	$("#ar_id").val(0);
	$("#title").val('');
	clearEditor('content');
	$("#small_title").val('');
	$("#tags").val('');
	$("#keywords").val('');
	$("#author").val('');
	$("#from").val('');
	$("#index_top").removeAttr('checked');
	$("#column_top").removeAttr('checked');
	$("#recommend").removeAttr('checked');
	$("#title_color").val('');
	$("#special_id").val('');
	$("#description").val('');
	$("#url_jump").val('');
	if($("#comment").attr('checked')==false){
		$("#comment2").removeAttr('checked');
		$("#comment").attr('checked','true');
	}
	$("#money").val('');
	$("#validate").attr('checked','true');
	$("#password").val('');
	$("#thumb_pic").val('');
	$("#draft").removeAttr('checked');
	$("#down_pic").removeAttr('checked');
	$("#get_first_pic").removeAttr('checked');
	var time=mydata();
	$("#add_time").val(time);
}

function view_thumb_pic(){}



function select_af(str){
	if(str[1]==1){
		$("#"+str[0]+'list').show();
	}else if(str[1]==0){
		$("#"+str[0]+'list').hide();
	}
}

function select_temp(str){
	$("#"+str[0]).attr("value",str[1]);
	$("#"+str[0]+'list').hide();
}

function insert_aft(str){
	$("#"+str[0]).attr("value",str[1]);
	$("#"+str[0]+'list').hide();
}

function select_co(){
	$("#color").show();
}

function color_select(str){
	$("#title_color").attr("value",str);
	sclose('color');
}



function update_author(){
	var value = $("#author").val();
	$.ajax({
		type: "POST",
		url: base_url+"?m=arhelper&a=saveauthor",
		data: {'author':value},
		dataType:'json',
		success: function(rs){
			if(rs.ok==1){
				$("#saveauthor").text("作者列表已保存！");
			}else{
				$("#saveauthor").text(rs.error);
			}
			$("#saveauthor").fadeIn();
			setTimeout(
				"$('#saveauthor').fadeOut(1000)",2000
			);
		}
	});
}

function update_from(){
	var value = $("#from").val();
	$.ajax({
		type: "POST",
		url: base_url+"?m=arhelper&a=savefrom",
		data: {'author':value},
		dataType:'json',
		success: function(rs){
			if(rs.ok==1){
				$("#savefrom").text("来源列表已保存！");
			}else{
				$("#savefrom").text(rs.error);
			}
			$("#savefrom").fadeIn();
			setTimeout(
				"$('#savefrom').fadeOut(1000)",2000
			);
		}
	});
}

function ar_validate(str){
	var ar = get_selected();
	if(ar=='')
	{
		alert('错误的操作，未选择任何运行参数！');
		return false;
	}
	var arString = ar.join('_');
	$.ajax({
		type: "POST",
		url: base_url+"?m=article&a=validate&tp="+str,
		data: {'ids':arString},
		dataType:'json',
		success: function(rs){
			if(rs.ok){
				for(x in ar){
					if(str=='validate'){
						$("#ar_"+ar[x]+" img").replaceWith("<img height='16' width='16' border='0' alt='是' src='images/admin/tick.png'/>")
					}else{
						$("#ar_"+ar[x]+" img").replaceWith("<img height='16' width='16' border='0' alt='否' src='images/admin/publish_x.png'/>")
					}
				}
			}
		}
	});
}

function ar_do(str){
	var ar = get_selected();
	if(ar==''){
		alert('错误的操作，未选择任何运行参数！');
		return false;
	}
	if(str=='delete'){
		if(confirm('此操作不可恢复,是否确定删除？')==false){
			return false;
		}
	}
	var arString = ar.join('_');
	window.location=base_url+'?m=article&a='+str+'&ids='+arString;
}

function ar_list_page(str){
	$("#sys_msg").text('数据加载中。。。').fadeIn();
	var url = str[0];
	var page = str[1];
	$.ajax({
		type: "POST",
		url: url,
		data: {'pg':page},
		dataType:'json',
		success: function(rs){
			$("#sys_msg").fadeOut();
			$(".mytable tr:gt(0)").remove();
			$(".mytable tr").after(rs.articles);
			$("#page_link").replaceWith(rs.page_link);
			onload_page();
		}
	});
}

function column_ar_list(){
	var cid = $("#ar_column").val();
	if(cid==0){
		return false;
	}else{
		window.location=base_url+'?m=article&a=index&type=cg&id='+cid;
	}
}

function search_article(){
	var rule = $("#search_rule").val();
	var key = $("#search_key").val();
	if(rule==0){
		alert('请选择筛选规则！');
		return false;
	}
	if(key==''){
		alert('筛选值不能为空！');
		return false;
	}
	window.location=base_url+'?m=article&a=search&rule='+rule+'&key='+key;
}

var move_articles = '';
function article_move_page(str){
	if(str==0){
		$("#article_move").hide();
		return true;
	}else{
		var ar = get_selected();
		if(ar==''){
			alert('错误的操作，未选择任何运行参数！');
			return false;
		}
		move_articles = ar.join('_');
		$("#article_move").show();
	}
}

function article_move(){
	var column = $("#move_column").val();
	if(column==0){
		alert('请选择要文章要移动到的栏目！');
		return false;
	}
	$("#article_move").hide();
	window.location=base_url+'?m=article&a=move&ids='+move_articles+'&cid='+column;
}